<template>
    <div class="flex justify-between items-center w-full mt-2">
        <el-checkbox v-model="isCheckAll" label="全选" size="large" />
        <div class="flex items-center">
            <div class=" mr-2"><span>已完成{{ count }}</span> / 全部{{ todos.length }}</div>
            <el-button type="primary" @click="clearCheckTodos()">清除已完成任务</el-button>
        </div>
    </div>
</template>

<script lang="ts">
import { tryOnScopeDispose } from '@vueuse/shared'
import { defineComponent, ref, computed} from 'vue'
import type { Todo } from '../../types/todo'

export default defineComponent({
    setup (props) {
        const isCheckAll = computed({
            get(){
                return count.value === props.todos.length && count.value > 0
            },
            set(val){
                props.checkAll(val)
            }
        })
        const count = computed(() => {
            return props.todos.reduce((pre, todo, index) => pre + (todo.isCompleted?1:0),0)
        })

        return {
            isCheckAll,
            count
        }
    },
    props: {
        todos: {
            type: Array as ()=> Todo [],
            required: true
        },
        checkAll: {
            type: Function,
            required: true
        },
        clearCheckTodos: {
            type: Function,
            required: true
        }
    }
})
</script>

<style scoped>

</style>